package com.rabbiter.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rabbiter.entity.User;
import com.rabbiter.mapper.UserMapper;
import com.rabbiter.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.util.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private MinioUtil minioUtil;

    @Override
    public IPage pageC(IPage<User> page) {
        return userMapper.pageC(page);
    }

    @Override
    public IPage pageCC(IPage<User> page, Wrapper wrapper) {
        return userMapper.pageCC(page,wrapper);
    }

    @Override
    public void saveAll(User user) {
        User insertUSer = new User();
        BeanUtils.copyProperties(user, insertUSer);
        //默认头像
        insertUSer.setAvatar("http://116.196.94.204:9000/warehouse/OIP-C.jpg");
        userMapper.insert(insertUSer);
    }

    @Override
    public String updateAvatar(Integer id, MultipartFile file) {
        try {
            User user = new User();
            String URL = minioUtil.uploadFile(file);

            user.setId(id);
            user.setAvatar(URL);
            userMapper.updateById(user);
            return URL;
        } catch (Exception e) {
            log.warn(e.getMessage());
            return null;
        }
    }
}
